// tradeUser.test.js
const BASE_URL = "http://127.0.0.1:1080/api";

// 测试分页查询贸易用户信息
async function testGetTradeUserPage() {
  try {
    const response = await fetch(
      `${BASE_URL}/trade-user/page?pageNum=1&pageSize=10`,
      {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    const result = await response.json();
    console.log("分页查询结果:", result);
    return result;
  } catch (error) {
    console.error("分页查询失败:", error);
  }
}

// 测试根据ID查询贸易用户信息
async function testGetTradeUserById(id) {
  try {
    const response = await fetch(`${BASE_URL}/trade-user/${id}`, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
    });

    const result = await response.json();
    console.log("根据ID查询结果:", result);
    return result;
  } catch (error) {
    console.error("根据ID查询失败:", error);
  }
}

// 测试创建贸易用户
async function testCreateTradeUser() {
  try {
    const userData = {
      userId: 1001,
      name: "测试用户",
      tel: "13800138000",
      email: "test@example.com",
      address: "测试地址",
      provinceCode: "110000",
      provinceName: "北京市",
      cityName: "北京市",
      cityCode: "110100",
      brief: "这是一个测试用户",
      registerTime: "2023-01-01 12:00:00",
      idCard: "110101199001011234",
      idCardFrontUrl: "http://example.com/front.jpg",
      idCardBackUrl: "http://example.com/back.jpg",
      tradeUserMode: 1,
      tradeUserStatus: 1,
      identityType: 1,
    };

    const response = await fetch(`${BASE_URL}/trade-user`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(userData),
    });

    const result = await response.json();
    console.log("创建用户结果:", result);
    return result;
  } catch (error) {
    console.error("创建用户失败:", error);
  }
}

// 测试更新贸易用户信息
async function testUpdateTradeUser(id) {
  try {
    const userData = {
      id: id,
      name: "更新后的用户",
      tel: "13900139000",
      email: "updated@example.com",
    };

    const response = await fetch(`${BASE_URL}/trade-user/updateTradeUser`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(userData),
    });

    const result = await response.json();
    console.log("更新用户结果:", result);
    return result;
  } catch (error) {
    console.error("更新用户失败:", error);
  }
}

// 测试删除贸易用户
async function testDeleteTradeUser(id) {
  try {
    const response = await fetch(
      `${BASE_URL}/trade-user/deleteTradeUser/${id}`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    const result = await response.json();
    console.log("删除用户结果:", result);
    return result;
  } catch (error) {
    console.error("删除用户失败:", error);
  }
}

// 测试更新贸易用户状态
async function testUpdateTradeUserStatus(id, status) {
  try {
    const response = await fetch(
      `${BASE_URL}/trade-user/updateTradeUserStatus/${id}?status=${status}`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    const result = await response.json();
    console.log("更新用户状态结果:", result);
    return result;
  } catch (error) {
    console.error("更新用户状态失败:", error);
  }
}

// 运行测试
async function runTests() {
  console.log("开始测试 TradeUser API...");

  // 测试分页查询
  await testGetTradeUserPage();

  // 测试创建用户
  const createUserResult = await testCreateTradeUser();
  const userId = createUserResult?.data?.id;

  if (userId) {
    // 测试根据ID查询
    await testGetTradeUserById(userId);

    // 测试更新用户
    await testUpdateTradeUser(userId);

    // 测试更新用户状态
    await testUpdateTradeUserStatus(userId, 2);

    // 测试删除用户
    await testDeleteTradeUser(userId);
  }
}

// 导出函数供其他模块使用
module.exports = {
  testGetTradeUserPage,
  testGetTradeUserById,
  testCreateTradeUser,
  testUpdateTradeUser,
  testDeleteTradeUser,
  testUpdateTradeUserStatus,
  runTests,
};

// 如果直接运行此文件，则执行测试
if (require.main === module) {
  runTests();
}
